package logic

import (
	"context"
	"fmt"
	mysqlinit "vipshop/models/mysql"
	"vipshop/models/redisinit"
	"vipshop/pkg"

	"vipshop/vipshoprpc/internal/svc"
	"vipshop/vipshoprpc/vipshoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *vipshoprpc.UpdateRequest) (*vipshoprpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	result1, _ := redisinit.Client.Get("SendSms" + in.Phone + "Update").Result()
	if in.SendSms != result1 {
		return nil, fmt.Errorf("短信验证码错误，请重新输入")
	}
	var register mysqlinit.User

	mysqlinit.DB.Debug().Where("id=?", register.ID).Find(&register)

	if register.ID != 0 {
		return nil, fmt.Errorf("手机号不存在，请重新输入")
	}

	if register.Password != pkg.Md5password(in.Password) {
		return nil, fmt.Errorf("密码，请重新输入")
	}

	if register.Password == in.NewPassword {
		return nil, fmt.Errorf("新密码不能与旧密码一致，请重新输入")
	}
	return &vipshoprpc.UpdateResponse{
		Id: int64(register.ID),
	}, nil
}
